#!/usr/bin/env groovy
// @file     TechVisionSigninParser.groovy
// @purpose  parser for sign-in records
// @author   Mac Radigan  

package ycoa.scan.signin

import org.codehaus.groovy.scriptom.*
import au.com.bytecode.opencsv.*
import java.text.*
import java.util.TimeZone

import ycoa.xml.*
import ycoa.service.*
import ycoa.session.*
import ycoa.markup.*
import ycoa.xml.*
import ycoa.scan.process.*
import ycoa.scan.signin.*
import ycoa.text.*

public class TechVisionSigninParser extends SigninParser { 
  public recordsetToDocument(signinRecordset) { 
    def document = XMLUtilities.newDocument("<recordset/>")
    signinRecordset.records.each() { record ->
    }
  }
  public printRecordset(signinRecordset) { 
    println "[listing records]"
    def index = 0
    signinRecordset.records.each() { record ->
      println "RECORD ${++index}> "
      println "  GLOBAL ${record.className}"
      println "  GLOBAL ${record.formPath}"
      println "  GLOBAL ${record.formName}"
      println "  classReportID: ${record.classReportID}"
      println "  className: ${record.className}"
      println "  studentID: ${record.studentID}"
      println "  studentName: ${record.studentName}"
      println "  feeType: ${record.feeType}"
      println "  parentName: ${record.parentName}"
      println "  signature: ${record.signature}"
      println "  present: ${record.present}"
      println "  makeup1: ${record.makeup1}"
      println "  makeup2: ${record.makeup2}"
      println "  makeup3: ${record.makeup3}"
      println "  makeupCount: ${record.getMakeups()}"
      println "  specialty: ${record.specialty}"
      println "  evaluation: ${record.evaluation}"
      println "  amountPaid: ${record.amountPaid}"
    }
  }
  public SigninRecordset parseOutput(csvfile) { 
    //def reader = new CSVReader(new FileReader(new File(csvfile)), ',', '"', 2)
    //def reader = new File(csvfile).text.split('\n')
    //def reader = new CSVReader(new FileReader(new File(csvfile)))
    def reader = new TextParser(csvfile)
    def keys = null
    def header = true
    def signinRecordset = new SigninRecordset()
    def signinRecord = null
    def index = 0
    def record = 0
    //def row = null
    //for(String excelRow : reader) {
    reader.readAll().each { row ->
      //row = excelRow.split(',')
      if(header) {
        keys = row
      } else if(verifyRow(row)) {
        def classReportID = 0 
        def className = ""
        def classDate = new Date()
        def formatter = new SimpleDateFormat("MMM dd. HH:mm a .")
        index = 0
        try {
          classReportID = Integer.parseInt(row[index++].trim())
        } catch(e) {
          println "record #${index}:  error reading Class Report ID"
        }
        try {
          className = row[index++].trim()
        } catch(e) {
          println "record #${index}:  error reading Class Name"
        }
        //signinRecord.classDate = (Date)formatter.parse(row[index++].trim())
        index++ // fix
        //println "reading ${keys[index]}: ${row[index]}"
        for(int recordCount=1; recordCount<=16; recordCount++) {
          // record set #N
          //println "record # ${++record}"
          signinRecord = new SigninRecord()
          signinRecord.classReportID = classReportID
          signinRecord.className = className
          //signinRecord.classDate = classDate
          //println "reading ${keys[index]}: ${row[index]}"
          try {
            signinRecord.studentID = Integer.parseInt(row[index++].trim())
          } catch(e) {
            println "record #${index}:  error reading student ID <${row[index]}>"
          }
          //println "reading ${keys[index]}: ${row[index]}"
          try {
            signinRecord.studentName = row[index++].trim()
          } catch(e) {
            println "record #${index}:  error reading student name"
          }
          try {
            signinRecord.feeType = row[index++].trim()
          } catch(e) {
            println "record #${index}:  error reading fee type"
          }
          try {
            signinRecord.parentName = row[index++].trim()
          } catch(e) {
            println "record #${index}:  error reading parent name"
          }
          try {
            signinRecord.signature = parseBoolean(row[index++].trim())
          } catch(e) {
            println "record #${index}:  error reading parent signature"
          }
          try {
            signinRecord.present = parseBoolean(row[index++].trim())
          } catch(e) {
            println "record #${index}:  error reading present"
          }
          try {
            signinRecord.makeup1 = parseBoolean(row[index++].trim())
          } catch(e) {
            println "record #${index}:  error reading makeup #1"
          }
          try {
            signinRecord.makeup2 = parseBoolean(row[index++].trim())
          } catch(e) {
            println "record #${index}:  error reading makeup #2"
          }
          try {
            signinRecord.makeup3 = parseBoolean(row[index++].trim())
          } catch(e) {
            println "record #${index}:  error reading makeup #3"
          }
          try {
            signinRecord.specialty = parseBoolean(row[index++].trim())
          } catch(e) {
            println "record #${index}:  error reading speialty"
          }
          try {
            signinRecord.evaluation = parseBoolean(row[index++].trim())
          } catch(e) {
            println "record #${index}:  error reading evaluation"
          }
          try {
            signinRecord.amountPaid = parseBoolean(row[index++].trim())
          } catch(e) {
            println "record #${index}:  error reading amount paid"
          }
          // record summary
          try {
            signinRecord.formPath = row[195].trim()
          } catch(e) {
            println "record #${index}:  error reading image path"
          }
          try {
            signinRecord.formName = row[196].trim()
          } catch(e) {
            println "record #${index}:  error reading image path"
          }
          signinRecordset.records.add(signinRecord)
        }
      }
      header = false
    }
    return signinRecordset
  } 
  public boolean parseBoolean(value) {
    if(value.toLowerCase().startsWith("y")) {
      return true
    } else {
      return false
    }
  }
  public boolean verifyRow(row) {
    try {
      return row[0].trim()!=''
    } catch(e) {
      return false
    }
  }
} 

// *EOF*
